<?php namespace Common\Model;
/**
 * 商品模型
 */
use Hdphp\Model\Model;
class Goods extends Model
{	
	protected $table = "goods";
	//自动验证
	protected $validate = array(
	   array('goods_category_id',"required","所属分类不能为空",3,3),
	   array('goods_brand_id',"required","所属品牌不能为空",3,3),
	   array('goods_name','required',"商品名称不能为空",3,3),
	   array('goods_num','required',"商品货号不能为空",3,3),
	   array('marketprice','required',"市场价不能为空",3,3),
	   array('shopprice','required',"商场价不能为空",3,3),
	   array('goods_total','required','总库存不能为空',3,3),
	   array('pic','required','请上传列表图',3,3),
	);
	//自动完成
	protected $auto = array(
	   array('time','time','function',3,1),
	   array('admin_id','user_id','method',3,1),
	);
	public function user_id()
	{
		return $_SESSION['admin_id'];
	}
	/**
	 * 添加
	 */
	public function store()
	{
		//实例化商品详情
		$detail = new \Common\Model\Detail;
		//自动验证
		if(!$this->create()) return false;
		if(!$detail->create())
		{
			$this->error = $detail->getError();
			return false;
		}
		//执行商品的添加
		$goodsaid = $this->add();
		//实例化商品属性
		$goodsattr = new \Common\Model\GoodsAttr;
		//添加商品表
		if(isset($_POST['spec'])){
		foreach($_POST['spec'] as $k=>$v)
		{
			foreach($v['spec'] as $kk=>$vv)
			{
				$data = array(
					"goods_value"=>$vv,
					"goods_added"=>$v['added'][$kk],
					"goodsattr_typeattr_id"=>$k,
					"goodsattr_goods_id"=>$goodsaid,
				);
				if($data['goods_value'])
				{
					$goodsattr->add($data);
				}				
			}
		}
		}
		if(isset($_POST['attr'])){
		foreach($_POST['attr'] as $key=>$value)
		{
			$data = array(
					"goods_value"=>$value,
					"goods_added"=>0,
					"goodsattr_typeattr_id"=>$key,
					"goodsattr_goods_id"=>$goodsaid,
				);
			if($data['goods_value'])
				{
					$goodsattr->add($data);
				}
		}
		}
		$small = "";
		$medium = "";
		$big = "";
		//处理商品图册
		foreach($_POST['goodsphoto'] as $k=>$v)
		{
			$smallpath = Image::thumb($v,'Upload/goods' . date('ymd') . '/smallthumb_' . basename($v),68,68,5);
			$small .= $smallpath . "|";
			$mediumpath = Image::thumb($v,'Upload/goods' . date('ymd') . '/mediumthumb_' . basename($v),450,585,5);
			$medium .= $mediumpath . "|";
			$bigpath = Image::thumb($v,'Upload/goods' . date('ymd') . '/bigthumb_' . basename($v),600,600,5);			
			$big .= $bigpath . "|";
		}
		$small = rtrim($small,'|');
		$medium = rtrim($medium,'|');
		$big = rtrim($big,'|');
		$detailData = array(
		   'small'=>$small,
		   'medium'=>$medium,
		   'big'=>$big,
		   'detail_goods_id'=>$goodsaid,
		   'intro'=>$_POST['intro'],
		   'service'=>$_POST['service'],
		);
		$detail->add($detailData);
		return true;
	}
    /**
	 * 编辑
	 */
	public function edit($goodsaid)
	{
		//实例化商品详情
		$detail = new \Common\Model\Detail;
		//自动验证
		if(!$this->create()) return false;
		if(!$detail->create())
		{
			$this->error = $detail->getError();
			return false;
		}
		//执行商品的修改
		$this->save();
		//实例化商品属性
		$goodsattr = new \Common\Model\GoodsAttr;
		//执行商品属性的删除
		$goodsattr->where('goodsattr_goods_id',$goodsaid)->delete();
		if(isset($_POST['attr']))
		{
			foreach($_POST['attr'] as $k=>$v)
			{
				$data = array(
				  "goods_value" => $v,
				  "goodsattr_typeattr_id" => $k,
				  "goodsattr_goods_id" => $goodsaid,
				  "goods_added" => 0,
				);
				if($data['goods_value'])
				{
					$goodsattr->add($data);
				}				
			}
		}
		if(isset($_POST['spec']))
		{
			foreach($_POST['spec'] as $k => $v)
			{
				foreach($v['spec'] as $kk=>$vv)
				{
				    $data = array(
				         "goods_value" => $vv,
				         "goodsattr_typeattr_id" => $k,
				         "goodsattr_goods_id" => $goodsaid,
				         "goods_added" => $v['added'][$kk],
				    );
					if($data['goods_value'])
				    {
					  $goodsattr->add($data);
				    }
				}
					
			}
		}
		//处理商品图册
		$small = "";
		$medium = "";
		$big = "";
		foreach($_POST['goodsphoto'] as $k=>$v)
		{
			$smallpath = Image::thumb($v,'Upload/goods' . date('ymd') . '/smallthumb_' . basename($v),68,68,5);
			$small .= $smallpath . "|";
			$mediumpath = Image::thumb($v,'Upload/goods' . date('ymd') . '/mediumthumb_' . basename($v),450,585,5);
			$medium .= $mediumpath . "|";
			$bigpath = Image::thumb($v,'Upload/goods' . date('ymd') . '/bigthumb_' . basename($v),600,600,5);			
			$big .= $bigpath . "|";
		}
		$small = rtrim($small,'|');
		$medium = rtrim($medium,'|');
		$big = rtrim($big,'|');
		$detailData = array(
		   'small'=>$small,
		   'medium'=>$medium,
		   'big'=>$big,
		   'detail_goods_id'=>$goodsaid,
		   'intro'=>$_POST['intro'],
		   'service'=>$_POST['service'],
		);
		$detail->where('detail_goods_id',$goodsaid)->update($detailData);
		return true;
	}
}
